//
//  HWModifyNiChenViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/11.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWModifyNiChenViewController.h"
#import "HWAES128.h"

@interface HWModifyNiChenViewController ()<UITextFieldDelegate>
@property(strong,nonatomic)UITextField *nametextField;
@property(strong,nonatomic)UIButton *baocunBut;  // 保存按钮

@end

@implementation HWModifyNiChenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUPUI];
    
    
}

// 设置UI
- (void)setUPUI {
    
    
       self.navigationItem.title = @"修改昵称";
      self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"修改昵称"];
    
    
       self.view.backgroundColor = [UIColor whiteColor];
    
 
        UIView *sousuoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 32)];
        UITextField *texfeild = [[UITextField alloc]initWithFrame:CGRectMake(15, NavBarHeight+15, kScreenW-15*2, 32)];
        texfeild.returnKeyType =UIReturnKeySearch;
        texfeild.delegate = self;
        texfeild.placeholder = @"输入你要修改的昵称";
        texfeild.placeholder  =  [HWjianzhiduiObject keylocalization:@"输入你要搜索的游戏"];
        //    texfeild.layer.borderColor  = RGB(208, 208, 208).CGColor;
        //    texfeild.layer.borderWidth = 0.5;
        texfeild.font = [UIFont systemFontOfSize:12];

        UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(5, 0, 30, 30);
    //    [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateNormal];
   //     [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateHighlighted];
        [sousuoView addSubview:button];
        texfeild.leftView = sousuoView;
        texfeild.leftViewMode = UITextFieldViewModeAlways;
        texfeild.layer.cornerRadius = 5;
        texfeild.layer.masksToBounds = YES;
        texfeild.backgroundColor = RGB(244, 242, 245);
        self.nametextField = texfeild;
        [self.view addSubview:self.nametextField];
    
    
    NSString *baocun = [HWjianzhiduiObject keylocalization:@"保存"];
    
        UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(15, texfeild.bottom+20, kScreenW-15*2, 30) Title:baocun Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap)];
          self.baocunBut = butt;
          butt.backgroundColor = RGB(38, 154, 255);
          butt.layer.cornerRadius = 15;
          butt.layer.masksToBounds = YES;
          [self.view addSubview:butt];
    
    
    
}

- (void)buttonTap {
    
    NSLog(@"保存");
    
    if (!(self.nametextField.text.length > 0)) {
        [MBProgressHUD showError:@"请输入修改的昵称"];
        return;
    } else if (!(self.nametextField.text.length < 9)) {
        
        [MBProgressHUD showError:@"昵称不能大于9个字符"];
        
         return;
    }
    
    [self xiugaitouxiang];
    
    
    
    
}








#pragma mark -----------    修改头像
- (void)xiugaitouxiang {
    
    
    
       //  NSString *Bete64 = [self encodeToBase64String:self.cunchuImage];
    
          NSString *userID = [HWLocallyStoredData getLoginUserID];
          NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
          NSString *signMD5 = [WMUtil md5HexDigest:sign];
          
          NSDictionary *signDic = @{
              @"pet_name" : self.nametextField.text
              
            
                                    };
          NSString *jsonStr = [self convertToJsonData:signDic];
          NSString *encryStr = [HWAES128 AES128Encrypt:jsonStr key:@"mtetbpryjhdxmnhx"];
    
    
    
    
          NSDictionary *parametes = @{
                                     @"ct" :  @"app",
                                     @"ac" : @"re_info",
                                     @"ios" : @"1",
                                     @"uid" :userID,
                                     @"t" :tStr,
                                     @"sign": signMD5,
                                      @"value" : encryStr,
                                     };
    
    
    
    
    
         
     //    [MBProgressHUD show:@"正在绑定邮箱"];
    
    
     [HWWebUtils doPostWithParameters:parametes progress:nil success:^(id response) {
        
                NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                //   [MBProgressHUD hideHUD];
                   
                   
                   if ([codeStr intValue] == 1) {
                     
                 
                      [MBProgressHUD  showSuccess:response[@"message"]];
                       //[self.navigationController popViewControllerAnimated:YES];
                       
                       [self.navigationController popViewControllerAnimated:YES];
                       
                    
                 
          
                      

                   } else {
                    
                        [MBProgressHUD  showError:response[@"message"]];
                       
                   }
                   NSLog(@"%@",response);
                   
        
    } failure:^(NSString *error) {

        [MBProgressHUD  showError:@"查询失败"];
    } ];
    
    
    
    
         
        
    
}


-(NSString *)convertToJsonData:(NSDictionary *)dict {
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}







@end
